package com.线程;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class 生产者消费者模式 {

     static  Object obj = new Object();

    static AtomicBoolean atomicBoolean = new AtomicBoolean(true);

    public static void main(String[] args) {



        Thread t =   new Thread(()->{

            synchronized (obj){

                Integer count = 0 ;

                while(true){

                    System.out.println(Thread.currentThread().getName()+":"+System.currentTimeMillis());

                    try {
                        Thread.sleep(1000);
                        if(atomicBoolean.get()) {
                            atomicBoolean.set(!atomicBoolean.get());
                            obj.notify();
                            obj.wait();
                        }
                    } catch (InterruptedException e) {
                        System.out.println("---");
                    }
                }
            }

        },"t1");

        Thread t2 =   new Thread(()->{
            synchronized (obj){

                Integer i = 0 ;

                while(true){

                    try {

                       System.out.println(Thread.currentThread().getName()+":"+System.currentTimeMillis());

                        Thread.sleep(1000);


                    if(!atomicBoolean.get()) {
                        atomicBoolean.set(!atomicBoolean.get());
                        obj.notify();
                        obj.wait();

                    }

                    } catch (InterruptedException e) {
                        e.printStackTrace();

                    }



                }
            }
        },"t2");

        t.start();
        t2.start();




    }


}
